package edu.xidian.bdilab.jrbigdata.service.bank.dealStatics.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.xidian.bdilab.jrbigdata.controller.bank.BankCode;
import edu.xidian.bdilab.jrbigdata.controller.government.GovernmentCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.DealProduct;
import edu.xidian.bdilab.jrbigdata.domain.FinancialProduct;
import edu.xidian.bdilab.jrbigdata.domain.ProductRequest;
import edu.xidian.bdilab.jrbigdata.mapper.*;
import edu.xidian.bdilab.jrbigdata.service.bank.dealStatics.DealStaticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Catfish
 * @date 2020/11/05
 */

@Service
public class DealStaticsServiceImpl implements DealStaticsService {

    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;

    @Autowired
    BankInfoMapper bankInfoMapper;

    @Autowired
    FinancialProductMapper financialProductMapper;

    @Autowired
    ProductRequestMapper productRequestMapper;

    @Autowired
    DealProductMapper dealProductMapper;

    @Override
    public ResponseResult getDealStatics(){
        ResponseResult responseResult = new ResponseResult();
        Map<String,Object> data=new HashMap<>(7);

        //获取所有企业数量
        Integer enterpriseCount= enterpriseInfoMapper.selectCount(null);
        //获取所有银行数量
        Integer bankCount= bankInfoMapper.selectCount(null);

        //获取所有金融产品
        List<FinancialProduct> financialProductList= financialProductMapper.selectList(null);
        //获取所有金融产品申请
        List<ProductRequest> productRequestList= productRequestMapper.selectList(null);
        //获取所有成交记录
        List<DealProduct> dealProductList= dealProductMapper.selectList(null);

        //金融产品数量
        Integer financialProductCount = financialProductList.size();
        //成交记录数量
        Integer dealProductCount = dealProductList.size();
        //总产品申请数量
        Integer productRequestCount = productRequestList.size()+dealProductCount;

        //总成交金额
        Integer dealProductAmountCount=0;
        for(DealProduct dealProduct: dealProductList){
            switch (dealProduct.getAmountLoanLimit()){
                case 0:dealProductAmountCount+=100;break;
                case 1:dealProductAmountCount+=200;break;
                case 2:dealProductAmountCount+=300;break;
                case 3:dealProductAmountCount+=500;break;
                default:dealProductAmountCount+=1000;break;
            }
        }

        //总申请金额
        Integer productRequestAmountCount=dealProductAmountCount;
        for(ProductRequest productRequest: productRequestList){
            //通过申请名查询指定产品额度
            FinancialProduct financialProduct = financialProductMapper
                    .selectById(productRequest.getFkProductName());
            switch (financialProduct.getLoanLimit()){
                case 0:productRequestAmountCount+=100;break;
                case 1:productRequestAmountCount+=200;break;
                case 2:productRequestAmountCount+=300;break;
                case 3:productRequestAmountCount+=500;break;
                default:productRequestAmountCount+=1000;break;
            }
        }

        data.put("enterpriseCount",enterpriseCount);
        data.put("bankCount",bankCount);
        data.put("financialProductCount",financialProductCount);
        data.put("productRequestCount",productRequestCount);
        data.put("productRequestAmountCount",productRequestAmountCount);
        data.put("dealProductCount",dealProductCount);
        data.put("dealProductAmountCount",dealProductAmountCount);

        responseResult.setData(data);
        responseResult.setMessage(BankCode.DEAL_STATICS_SUCCESS.getMessage());
        responseResult.setSuccess(true);
        responseResult.setCode(BankCode.DEAL_STATICS_SUCCESS.getCode());

        return responseResult;
    }

    @Override
    public ResponseResult getDealProduct(Integer pageNum,Integer pageSize,String bankName,String productName,String enterpriseName,String socialCode){
        ResponseResult responseResult=new ResponseResult();
        QueryWrapper<DealProduct> dealProductQueryWrapper=new QueryWrapper<>();
        PageHelper.startPage(pageNum,pageSize);
        try {
            dealProductQueryWrapper.like(bankName!=null,"bank_name",bankName);
            dealProductQueryWrapper.like(productName!=null,"product_name",productName);
            dealProductQueryWrapper.like(enterpriseName!=null,"enterprise_name",enterpriseName);
            dealProductQueryWrapper.like(socialCode!=null,"social_code",socialCode);
            List<DealProduct> dealProducts=dealProductMapper.selectList(dealProductQueryWrapper);
            PageInfo pageInfo=new PageInfo<> (dealProducts);
            Map<String,Object> data=new HashMap<>(3);
            data.put("dealProducts List",dealProducts);
            data.put("Total Page Num",pageInfo.getPages());
            data.put("Total",pageInfo.getTotal());
            responseResult.setCode(GovernmentCode.NAME_REQ_SUCCESS.getCode());
            responseResult.setMessage("查看成交记录成功");
            responseResult.setData(data);
            responseResult.setSuccess(true);
            return responseResult;
        }catch (Exception e){
            responseResult.setCode(GovernmentCode.NAME_REQ_FAIL.getCode());
            responseResult.setMessage("查看成交记录失败");
            responseResult.setSuccess(false);
            return responseResult;
        }
    }
}
